import type { FormOptions } from '@/components/ui/form/src/types/type'
import { USER_ROLE, USER_STATUS } from '@/config/map'
import { FormSearchFilter } from '@/utils'

const accountFormOptions: FormOptions[] = [
  {
    attrs: {
      allowClear: true,
    },
    label: 'User name',
    prop: 'username',
    required: 'username',
    rules: [
      {
        message: 'Please enter the username',
        required: true,
        trigger: 'blur',
      },
      {
        min: 2,
        max: 15,
        message: 'The username is between 2 and 15 characters',
        trigger: 'blur',
      },
    ],
    type: 'input',
    value: '',
  },
  {
    attrs: {
      allowClear: true,
    },
    label: 'Password',
    prop: 'password',
    // required: 'password',
    type: 'input-password',
    value: '',
    rules: [
      {
        message: 'Please enter the password',
        // required: true,
        trigger: 'blur',
      },
      {
        min: 6,
        message: 'The password must be at least 6 characters long.',
        trigger: 'blur',
      },
    ],
  },
  {
    attrs: {
      allowClear: true,
    },
    label: 'nickName',
    prop: 'nickName',
    type: 'input',
    value: '',
  },
  {
    attrs: {
      mode: 'multiple',
      allowClear: true,
      style: {
        width: '100%',
      },
    },
    children: [
      {
        label: 'admin',
        type: 'select-option',
        value: USER_ROLE.admin,
      },
      {
        label: 'user',
        type: 'select-option',
        value: USER_ROLE.user,
      },
    ],
    label: 'User Role',
    placeholder: 'Please select a role',
    prop: 'roles',
    required: 'roles',
    rules: [
      {
        message: 'Please select a role',
        required: true,
        trigger: 'change',
      },
    ],
    type: 'select',
    value: [],
  },
  {
    attrs: {
      allowClear: true,
      style: {
        width: '100%',
      },
    },
    children: [
      {
        label: 'normal',
        type: 'select-option',
        value: USER_STATUS.normal,
      },
      {
        label: 'disabled',
        type: 'select-option',
        value: USER_STATUS.disabled,
      },
    ],
    label: 'Status',
    placeholder: 'Please select a status',
    prop: 'isFrozen',
    required: 'isFrozen',
    rules: [
      {
        message: 'Please select a status',
        required: true,
        trigger: 'change',
      },
    ],
    type: 'select',
    value: USER_STATUS.normal,
  },
  {
    attrs: {
      allowClear: true,
    },
    label: 'email',
    prop: 'email',
    type: 'input',
    value: '',
  },
  {
    attrs: {
      allowClear: true,
    },
    label: 'captcha',
    prop: 'captcha',
    type: 'input',
    value: '',
    tooltip: '目前支持163邮箱,同时需要打开163邮箱里的POP3/SMTP服务(邮件服务使用的是nodemailer包)',
  },
]

const searchFormOptions: FormOptions[] = [
  {
    type: 'select',
    value: null,
    label: 'Name',
    prop: 'username',
    placeholder: 'Please select the user name',
    attrs: {
      'allowClear': true,
      'show-search': true,
      'filter-option': FormSearchFilter,
      'style': {
        width: '100%',
      },
    },
    children: [],
  },
  {
    type: 'select',
    value: [],
    label: 'Roles',
    prop: 'roles',
    placeholder: 'Please select the user roles',
    attrs: {
      'mode': 'multiple',
      'allowClear': true,
      'show-search': true,
      'filter-option': FormSearchFilter,
      'style': {
        width: '100%',
      },
    },
    children: [
      {
        label: 'ADMIN',
        type: 'select-option',
        value: 'admin',
      },
      {
        label: 'USER',
        type: 'select-option',
        value: 'user',
      },
    ],
  },
  {
    type: 'select',
    value: null,
    label: 'Status',
    prop: 'isFrozen',
    placeholder: 'Please select the user status',
    attrs: {
      'allowClear': true,
      'show-search': true,
      'filter-option': FormSearchFilter,
      'style': {
        width: '100%',
      },
    },
    children: [
      {
        label: 'Normal',
        type: 'select-option',
        value: '0',
      },
      {
        label: 'Disabled',
        type: 'select-option',
        value: '1',
      },
    ],
  },
  {
    type: 'input',
    value: null,
    label: 'Email',
    prop: 'email',
    placeholder: 'Please input the user email',
    attrs: {
      allowClear: true,
      style: {
        width: '100%',
      },
    },
  },
]

export const indexConfig = {
  columns: [
    { align: 'center', dataIndex: 'username', title: 'User Name' },
    { align: 'center', dataIndex: 'roles', title: 'User Role', customRender: ({ text }: { text: string[] }) => {
      return text.join(',')
    } },
    { align: 'center', dataIndex: 'isFrozen', title: 'Status', customRender: ({ text }: { text: string }) => {
      return text ? 'Disabled' : 'Normal'
    } },
    { align: 'center', dataIndex: 'updateTime', title: 'updateTime' },
    { dataIndex: 'action', title: 'Acticon' },
  ],
  text: {
    searchAction: 'Search',
    addAction: 'New',
    addModalTitle: 'New User',
    editAction: 'Edit',
    editModalTitle: 'Update User',
    popNoText: 'No',
    popOkText: 'Yes',
    disableAction: 'Disabled',
    enableAction: 'Enabled',
    confirmDisableInfo: 'Are you sure disabled this user?',
    confirmEnableInfo: 'Are you sure enable this user?',
  },
  options: accountFormOptions,
  searchFormOptions,
}
